package io.renren.utils.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;

import io.renren.utils.util.db.connection.DBConnectionManager;
import io.renren.utils.util.db.connection.DataSourceFactory;
import io.renren.utils.util.db.connection.LocalDataSourceFactory;
import io.renren.utils.util.exception.XMTException;

public class DatabaseUtil {

	static Logger logger = Logger.getLogger(DatabaseUtil.class.getName());
	
	static DataSourceFactory dsf = new LocalDataSourceFactory();
	
	public static Connection getConnection(String dbName) throws XMTException {
		Connection connection = null;
		try {
			// Use connection pool myself
			connection = DBConnectionManager.getInstance().getConnection(dbName);
			// Or use apache db connection pool 
			//connection = dsf.getDataSource(dbName).getConnection();
			
	    	if (logger.isDebugEnabled()) {
	    		logger.debug("Get connection : "+ connection.hashCode());
	    	}
		} catch (Exception e1) {
			e1.printStackTrace();
			throw new XMTException("", Error.SYS_OPEN_CONNECTION_ERROR.getErrorCode());
		}
		return connection;
	}

	public static Connection getConnection() throws XMTException {
			return getConnection(SystemConfigUtil.getInstance().getProperty("DBName"));
	}


	public static void closeConnection(Connection connection) {
		try {
			if (connection != null) {
				connection.close();
				connection = null;
			}
		} catch (Exception sqlexception1) {
			sqlexception1.printStackTrace();
		}
	}
   /**
     * 关闭ResultSets
     */
    public static void closeResultSetAndStatement(ResultSet resultSet, Statement statement) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (statement != null) {
            try {
            	statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }                
    }
	
   /**
     * 关闭ResultSets
     */
    public static void closeResultSets(ResultSet... resultSets) {
        if (resultSets != null && resultSets.length > 0) {
            for (ResultSet resultSet : resultSets) {
                if (resultSet != null) {
                    try {
                        resultSet.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    /**
     * 关闭Statements
     */
    public static void closeStatements(Statement... statements) {
        if (statements != null && statements.length > 0) {
            for (Statement statement : statements) {
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    
    /**
     * 关闭SQL PreparedStatement
     * @param prestmts
     */
    public static void closePreparedStatement(PreparedStatement... prestmts){
    	if (prestmts != null && prestmts.length > 0) {
            for (PreparedStatement c : prestmts) {
                if (c != null) {
                    try {
                        c.close();
                    } catch (Exception f) { 
                        f.printStackTrace();
                    }
                }
            }
        }
    }
}
